
//
//  VideoDescCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/5.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "VideoDescContentCell.h"

@interface VideoDescContentCell ()<UITextViewDelegate>
@property (nonatomic,strong)UITextView *contentTF;
@property (nonatomic,strong)UILabel *placeLb;
@property (nonatomic,strong)UIView *coverView;
@property (nonatomic,strong)UILabel *countLb;
@property (nonatomic,assign)NSInteger maxCount;
@end

@implementation VideoDescContentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}

- (void)setUI{
    self.contentTF = [[UITextView alloc]init];
    self.contentTF.textColor = C51;
    self.contentTF.font = font(14);
    self.contentTF.tintColor = RGBA(245, 75, 100, 1);
    self.contentTF.delegate = self;
    [self.contentView addSubview:self.contentTF];
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).offset(-16);
        make.top.equalTo(self.contentView).offset(10);
        make.left.equalTo(self.contentView).offset(16);
        make.bottom.equalTo(self.contentView).offset(-21);
    }];
    self.placeLb = [[UILabel alloc]init];
    self.placeLb.font = font(14);
    self.placeLb.text = @"描述问题的详细情况，有助于我们快速帮您解决";
    self.placeLb.textColor = RGBA(204, 204, 204, 1);
    [self.contentView addSubview:self.placeLb];
    [self.placeLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(18);
        make.left.equalTo(self.contentView).offset(21);
    }];
    
    self.coverView = [[UIView alloc]init];
    self.coverView.backgroundColor = RGBA(204, 204, 204, 1);
    self.coverView.layer.cornerRadius = 7;
    self.coverView.layer.masksToBounds = YES;
    [self.contentView addSubview:self.coverView];
    [self.coverView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(35, 14));
        make.bottom.equalTo(self.contentView).offset(-10);
        make.right.equalTo(self.contentView).offset(-16);
    }];
    
    self.countLb = [[UILabel alloc]init];
    self.countLb.textColor = [UIColor whiteColor];
    self.countLb.text = @"100";
    self.countLb.font = font(12);
    [self.contentView addSubview:self.countLb];
    [self.countLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.coverView);
    }];
}


+(instancetype)cellWithTableView:(UITableView *)tableView{
    NSString *ID = @"VideoDescContentCell";
    VideoDescContentCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[VideoDescContentCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}
- (void)setDescContent:(NSString *)content{
    [self setDescContent:content maxCount:100 isMine:NO];
}

- (void)setDescContent:(NSString *)content maxCount:(NSInteger)maxCount isMine:(BOOL)isMine{
    self.placeLb.hidden = content.length > 0;
    self.contentTF.text = content;
    if (content.length > maxCount) {
        self.contentTF.text = [content substringToIndex:maxCount];
    }
    self.countLb.text = [NSString stringWithFormat:@"%lu",maxCount - content.length];
    
    self.maxCount = maxCount;
    
    if (isMine) {
        self.placeLb.text = @"请输入问题描述";
    }
}

#pragma mark - textViewDelegate
- (void)textViewDidChange:(UITextView *)textView{
    
    NSString *toBeString = textView.text;
 
    //获取高亮部分
    UITextRange *selectedRange = [textView markedTextRange];
    UITextPosition *position = [textView positionFromPosition:selectedRange.start offset:0];
 
    // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > self.maxCount){
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.maxCount];
            if (rangeIndex.length == 1){
                textView.text = [toBeString substringToIndex:self.maxCount];
            }else{
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.maxCount)];
                textView.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
    
    self.placeLb.hidden = textView.text.length > 0;
    if (textView.text.length > self.maxCount) {
        self.countLb.text = @"0";
    }else{
        self.countLb.text = [NSString stringWithFormat:@"%lu",self.maxCount - textView.text.length];
    }
    if (self.delegate && [self.delegate respondsToSelector:@selector(videoDescDidChange:)]) {
        [self.delegate videoDescDidChange:textView.text];
    }
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView{
    if (self.delegate && [self.delegate respondsToSelector:@selector(videoDescContentEndEditing:)]) {
        [self.delegate videoDescContentEndEditing:textView.text];
    }
    return YES;
}


@end
